SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE PROCEDURE dbo.[DnnProduct_CoreMessaging_GetNextMessagesForDispatch]
	@SchedulerInstance uniqueidentifier,
	@BatchSize int
AS
BEGIN
	WITH CTE AS
	(
		SELECT TOP (@BatchSize) *
		FROM DnnProduct_CoreMessaging_MessageRecipients
		WHERE EmailSent = 0 AND (EmailSchedulerInstance IS NULL OR EmailSchedulerInstance = '00000000-0000-0000-0000-000000000000')
		ORDER BY CreatedOnDate DESC
	)
	UPDATE CTE
	SET EmailSchedulerInstance = @SchedulerInstance
	
	SELECT TOP (@BatchSize) *
	FROM DnnProduct_CoreMessaging_MessageRecipients
	WHERE EmailSent = 0 AND EmailSchedulerInstance = @SchedulerInstance
	ORDER BY CreatedOnDate DESC
END
GO
